﻿using System.Web.Mvc;
using BLL.Security;
using BLL.Services.Books;
using BLL.Services.Categories;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using Web.Infrastructure;
using Web.Models;

namespace Web.Controllers
{
    public partial class CategoriesController : FrontendController
    {
        private readonly IBookService _bookService;

        private readonly IEnvironmentContext _environmentContext;

        private readonly ICategoriesService _categoriesService;

        public CategoriesController(IBookService bookService, IEnvironmentContext environmentContext, ICategoriesService categoriesService)
        {
            _bookService = bookService;
            _environmentContext = environmentContext;
            _categoriesService = categoriesService;
        }

        [HttpGet]
        public virtual ActionResult IndexTemplate()
        {
            return BackBoneView(MVC.Categories.Views.Index);
        }

        [HttpGet]
        public virtual ActionResult ListingTemplateIconView()
        {
            return BackBoneView(MVC.Categories.Views.ListingIconView);
        }

        [HttpGet]
        public virtual ActionResult ListingTemplateListView()
        {
            return BackBoneView(MVC.Categories.Views.ListingListView);
        }

        public virtual ActionResult Index(
            int? categoryId, GridSortOptions sort, CategoryBookModel.CategoryBookViewType? viewType)
        {
            sort.Column = "Name";
            if (categoryId.HasValue)
            {
                var user = _environmentContext.User;
                var userId = user != null ? user.UserId : 0;

                var books = _bookService.GetSomeBooksByCategory(
                    categoryId.Value, userId, sort.Column, sort.Direction != SortDirection.Descending ? 0 : 1);

                var model = new CategoryBookModel(books);

                return BackBoneJson(model.GetData(viewType ?? CategoryBookModel.CategoryBookViewType.Icon));
            }
            return BackBoneJson(string.Empty);
        }

        public virtual ActionResult Listing(
            int? categoryId, int? page, GridSortOptions sort, CategoryBookModel.CategoryBookViewType viewType)
        {
            if (categoryId.HasValue && page.HasValue)
            {
                var user = _environmentContext.User;
                var userId = user != null ? user.UserId : 0;

                var books = _bookService.GetSomeBooksByCategory(
                        categoryId.Value, userId, sort.Column, sort.Direction != SortDirection.Descending ? 0 : 1, page.Value);

                var model = new CategoryBookModel(books);

                return BackBoneJson(model.GetData(viewType));
            }
            return BackBoneJson(string.Empty);
        }

        public virtual ActionResult Navigation()
        {
            var categories = _categoriesService.GetAllCategories;
            return BackBoneJson(categories);
        }

        public virtual ActionResult NavigationTemplate()
        {
            return BackBoneView(MVC.Categories.Views.NavigationTemplate);
        }

        public virtual ActionResult AlphabetTemplate()
        {
            return this.BackBoneView(MVC.Categories.Views.Alphabet);
        }
    }
}